import 'dart:convert';
import 'package:eh_mobile/common/utils/extension.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:logger/logger.dart';
import '../../pages/main/homepage/model/home_device_model.dart';
import '../../pages/main/smart_home_info/model/device_dynamic_model.dart';
import '../store/user_store.dart';

class WebRtcChannelCommon {
  //发送校验身份信息
  static sendCheckIDInfo(RTCDataChannel? sendChannel) {
    Map jsonMap = {'type': 'token', 'token': UserStore().mobileToken};
    String jsonStr = jsonEncode(jsonMap);
    sendChannel!.send(
      RTCDataChannelMessage(jsonStr),
    );
  }

//发送信息获取底部分类数据
  static String sendInfoGetCategoryList(RTCDataChannel? sendChannel,{String? userId}) {
    //获取底部栏数据
    String randomStr = '';
    randomStr = randomStr.generateRandomString(8);
    Map categoryListMap = {
      'type': 'ask',
      'index': randomStr,
      'path': 'category/list',
      'params': {'user_id': userId, 'mobile': UserStore().mobileToken}
    };
    String jsonCategoryListStr = jsonEncode(categoryListMap);
    Logger().d('发送底部栏Map给远端：$categoryListMap');
    sendChannel!.send(
      RTCDataChannelMessage(jsonCategoryListStr),
    );
    return randomStr;
  }

  //发送信息获取用户名字
  static String sendChannelGetUserName(RTCDataChannel? sendChannel) {
    //获取系统列表名字
    String sysListRandomStr = '';
    sysListRandomStr = sysListRandomStr.generateRandomString(8);
    Map systemListMap = {
      'type': 'ask',
      'index': sysListRandomStr,
      'path': 'dynamic/get',
      'params': {}
    };
    String jsonSystemListStr = jsonEncode(systemListMap);
    sendChannel!.send(
      RTCDataChannelMessage(jsonSystemListStr),
    );
    return sysListRandomStr;
  }

  //拿到底部栏分类数据
  static getHomeBottomCateGoryList(Map jsonMap, Function? receiveDataBlock) {
    HomeDeviceModel deviceModel = HomeDeviceModel.fromJson(jsonMap);
    if (receiveDataBlock != null) {
      receiveDataBlock(deviceModel);
    }
  }

//获取首页动态列表
  static getHomeDynamicDeviceList(Map jsonMap, Function? receiveDataBlock) {
    DeviceDynamicModel systemUserNameModel =
    DeviceDynamicModel.fromJson(jsonMap);
    if (receiveDataBlock != null) {
      receiveDataBlock(systemUserNameModel);
    }
  }

  static getDynamicCmd(Map jsonMap, Function? receiveDataBlock) {
    DeviceDynamicModelCmd systemUserNameModel =
    DeviceDynamicModelCmd.fromJson(jsonMap);
    if (receiveDataBlock != null) {
      receiveDataBlock(systemUserNameModel);
    }
  }

  //发送信息控制设备
  static sendChannelDeviceWrite(RTCDataChannel? sendChannel,
      {HomeDeviceModelDevices? item, Map? dataMap}) {
    String sysListRandomStr = '';
    sysListRandomStr = sysListRandomStr.generateRandomString(8);
    Map systemListMap = {
      'type': 'ask',
      'index': sysListRandomStr,
      'path': 'device/write',
      'params': {'dev_id': item?.devId, 'data': dataMap}
    };
    String jsonSystemListStr = jsonEncode(systemListMap);
    Logger().d('控制设备：$systemListMap');
    sendChannel!.send(
      RTCDataChannelMessage(jsonSystemListStr),
    );
  }

  //拿到操作信息报告命令
  static getReportCmdData(Map dataMap, Function? receiveDataBlock) {
    if (receiveDataBlock != null) {
      receiveDataBlock(dataMap);
    }
  }
}
